package com.uenpay.im.weight;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.uenpay.im.R;

/**
 * 
 * Class Explain:含有输入框的dialog
 * 
 * @author SL
 * @Create Date: 2014-9-20 下午5:35:24
 * @copyRight http://www.qiyunxin.com
 */
public class MyDialogEditText extends Dialog {

	public interface IButtonOnClickListener {
		public abstract void onClick(MyDialogEditText mMyDialogEditText,
									 String editContent);
	}

	public MyDialogEditText(Context context) {
		super(context);
	}

	public MyDialogEditText(Context context, int theme) {
		super(context, theme);
	}

	public static class Builder {
		private Context context;
		private String message;
		private View contentView;
		private EditText edittext;
		private IButtonOnClickListener mIButtonOnClickListener;
		private Dialog mDialog;

		public Builder(Context context) {
			this.context = context;
		}

		public Builder setMessage(String message) {
			this.message = message;
			return this;
		}

		/**
		 * Set the Dialog message from resource
		 * 
		 * @param message
		 * @return
		 */
		public Builder setMessage(int message) {
			this.message = (String) context.getText(message);
			return this;
		}

		public Builder setPositiveButton(IButtonOnClickListener listener) {
			this.mIButtonOnClickListener = listener;
			return this;
		}

		public Builder setContentView(View v) {
			this.contentView = v;
			return this;
		}

		public Dialog getDialog() {
			return mDialog;
		}

		@SuppressLint("NewApi")
		public MyDialogEditText create() {
			final MyDialogEditText dialog = new MyDialogEditText(context,
					R.style.dialog);
			FrameLayout layout = (FrameLayout) View.inflate(context,
					R.layout.dialog_edittext_layout, null);
			edittext = (EditText) layout.findViewById(R.id.edittext);
			dialog.addContentView(layout, new LayoutParams(
					LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

			if (mIButtonOnClickListener != null) {
				((Button) layout.findViewById(R.id.sure_btn))
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								String editContent = edittext.getText()
										.toString();
								mIButtonOnClickListener.onClick(dialog,
										editContent);
							}
						});

				((Button) layout.findViewById(R.id.cancel_btn))
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								dialog.dismiss();
							}
						});
				showSoftKeyboard(edittext);
				edittext.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence arg0, int arg1,
							int arg2, int arg3) {
						String content = edittext.getText().toString();
						if (Utils.String_length(content) > 50) {
							if (content.length() > 25) {
								edittext.setText(content.substring(0, 25));
							}
							return;
						}
					}

					@Override
					public void beforeTextChanged(CharSequence arg0, int arg1,
							int arg2, int arg3) {
					}

					@Override
					public void afterTextChanged(Editable arg0) {
					}
				});
			}
			if (message != null) {
				((TextView) layout.findViewById(R.id.message)).setText(message);
			} else if (contentView != null) {
				((LinearLayout) layout.findViewById(R.id.content_layout))
						.removeAllViews();
				((LinearLayout) layout.findViewById(R.id.content_layout))
						.addView(contentView, new LayoutParams(
								LayoutParams.MATCH_PARENT,
								LayoutParams.WRAP_CONTENT));
				((LinearLayout) layout.findViewById(R.id.content_layout))
						.setGravity(Gravity.CENTER_HORIZONTAL);

			}
			dialog.setContentView(layout);
			mDialog = dialog;
			return dialog;
		}
		
		private void showSoftKeyboard(EditText sendEdit) {
			InputMethodManager inputManager = (InputMethodManager) context
					.getSystemService(Context.INPUT_METHOD_SERVICE);
			((Activity) context).getWindow().setSoftInputMode(
					WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

			inputManager
					.showSoftInput(sendEdit, InputMethodManager.HIDE_NOT_ALWAYS);
		}
	}
	

}
